package com.infore.siteManagement.service.ddic.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.constant.ResultConstants;
import com.infore.siteManagement.entity.ddic.DdicFalutType;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.ddic.IFalutTypeRepository;
import com.infore.siteManagement.select.QueryDispatcher;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.QueryUtil;
import com.infore.siteManagement.service.ddic.IFalutTypeService;
import com.infore.siteManagement.util.ArrayUtils;
import com.infore.siteManagement.util.MyBeanUtils;
import com.infore.siteManagement.util.Page;
import com.infore.siteManagement.util.PageUtil;
import com.infore.siteManagement.util.ResultEntity;

@Service
public class FalutTypeServiceImpl implements IFalutTypeService {

	@Autowired
	private IFalutTypeRepository iFalutTypeRepository;
	@Autowired
	private QueryDispatcher queryDispatcher;

	public ResultEntity query(QueryParamDto queryParamDto) {
		if (org.springframework.util.StringUtils.isEmpty(queryParamDto)) {// 无任何条件查询
			List<DdicFalutType> list = iFalutTypeRepository.findAll();
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
			// 仅分页查询
		} else if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject())
				&& org.springframework.util.StringUtils.isEmpty(queryParamDto.getQueryCondition())
				&& org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleCondition())
				&& org.springframework.util.StringUtils.isEmpty(queryParamDto.getAssembleSql())) {
			org.springframework.data.domain.Page<DdicFalutType> pageable = iFalutTypeRepository
					.findAll(queryParamDto.getPageObject().getPageRequest());
			Page<DdicFalutType> page = PageUtil.pageConvert(pageable);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
			// 只传属性不传字段的查询
		} else if (org.apache.commons.lang.StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) {
			return this.queryAll(queryParamDto);
		}
		// 指定字段查询
		return queryDispatcher.dispatcher(queryParamDto, DdicFalutType.class);
	}

	@SuppressWarnings("serial")
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		// 判断是否有条件再判断是否需要分页,不分页必须传null
		if (!org.springframework.util.StringUtils.isEmpty(queryParamDto.getPageObject())) {
			// 需要分页
			org.springframework.data.domain.Page<DdicFalutType> pageAble = iFalutTypeRepository
					.findAll(new Specification<DdicFalutType>() {
						public Predicate toPredicate(Root<DdicFalutType> root, CriteriaQuery<?> query,
								CriteriaBuilder criteriaBuilder) {
							// 构建条件
							List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil
									.createQueryCondition(queryParamDto, criteriaBuilder, root);
							// 组装条件
							Predicate predicate = com.infore.siteManagement.select.util.QueryUtil
									.assembleCondition(queryParamDto, criteriaBuilder, predicates);
							QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
							return query.getRestriction();
						}
					}, queryParamDto.getPageObject().getPageRequest());
			Page<DdicFalutType> page = PageUtil.pageConvert(pageAble);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		} else {
			List<DdicFalutType> list = iFalutTypeRepository.findAll(new Specification<DdicFalutType>() {
				public Predicate toPredicate(Root<DdicFalutType> root, CriteriaQuery<?> query,
						CriteriaBuilder criteriaBuilder) {
					// 构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil
							.createQueryCondition(queryParamDto, criteriaBuilder, root);
					// 组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil
							.assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			});
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
		}
	}

	public ResultEntity delete(String ids) {
		try {
			List<String> idsList = ArrayUtils.stringToList(ids);
			iFalutTypeRepository.deleteByIdIn(idsList);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultEntity(ResultStatus.SUCCESS, Constants.DELETE_FAILUE);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.DELETE_SUCCESS);
	}

	public ResultEntity edit(DdicFalutType ddicFalutType) {
		String id = ddicFalutType.getId();
		if (StringUtils.isNotBlank(id)) {
			DdicFalutType ddicFalutTypeOld = iFalutTypeRepository.findById(id).orElse(null);
			if (ddicFalutTypeOld == null) {
				return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_SUCH_OBJECT);
			}
			MyBeanUtils.copyNonNullProperties(ddicFalutType, ddicFalutTypeOld);
			ddicFalutType = ddicFalutTypeOld;
		}
		try {
			iFalutTypeRepository.saveAndFlush(ddicFalutType);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_FAILUE);
		}
		if (StringUtils.isNotBlank(id)) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.UPDATE_SUCCESS);
		} else {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
		}
	}

}
